#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>

//循环栈最大存放MaxSize-1个元素
#define MaxSize 15
typedef char ElemType;

typedef struct
{
    ElemType data[MaxSize]; //存放队中元素
    int front, tail;        //队头和队尾指针
} SqQueue;

void InitQueue(SqQueue *&q) //初始化队列
{
    q = (SqQueue *)malloc(sizeof(SqQueue));
    q->front = q->tail = 0;
}

void DestoryQueue(SqQueue *&q) //销毁队列
{
    free(q);
}

bool QueueEmpty(SqQueue *q) //判断是否空队列
{
    return q->front == q->tail;
}

bool QueueFull(SqQueue *q) //判断队列是否已满
{
    return (q->tail + 1) % MaxSize == q->front;
}

bool EnterQueue(SqQueue *&q, ElemType e) //进队列
{
    if ((q->tail + 1) % MaxSize == q->front)
        return false;
    q->tail = (q->tail + 1) % MaxSize;
    q->data[q->tail] = e;
    return true;
}

bool OutOfQueue(SqQueue *&q, ElemType &outchar) //出队列
{
    if (q->front == q->tail)
        return false;
    q->front = (q->front + 1) % MaxSize;
    outchar = q->data[q->front];
    return true;
}

int QueueCount(SqQueue *q) //求队列中元素个数
{
    return (q->tail - q->front + MaxSize) % MaxSize;
}

int main()
{
    SqQueue *q;
    InitQueue(q);
    if (QueueEmpty(q))
        printf("此时队列为空\n");
    else
        printf("此时队列不为空\n");
    EnterQueue(q, 'a');
    EnterQueue(q, 'b');
    EnterQueue(q, 'c');

    ElemType outchar = '\0'; //储存出队元素
    if (OutOfQueue(q, outchar))
        printf("出队成功，出队元素为：%c\n", outchar);
    else
        printf("出队失败，该队列为空！\n");
    EnterQueue(q, 'd');
    EnterQueue(q, 'e');
    EnterQueue(q, 'f');

    //输出出队序列
    printf("出队序列为：");
    while (!QueueEmpty(q))
    {
        OutOfQueue(q, outchar);
        printf("%c ", outchar);
    }
    DestoryQueue(q);
    return 0;
}